package com.erp.base.common.service.impl;

import java.io.File;

import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.spring5.SpringTemplateEngine;

import com.erp.base.common.entity.Email;
import com.erp.base.common.service.MailService;

@Service
public class MailServiceImpl implements MailService {
	private static Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
	@Autowired
	private JavaMailSender mailSender;// 执行者
	@Autowired
	private SpringTemplateEngine templateEngine;// thymeleaf
	@Value("${spring.mail.username}")
	public String USER_NAME;// 发送者


	/**
	 * 发送一个简单格式的邮件
	 *
	 * @param mailBean
	 */
	@Async
	@Override
	public void sendSimpleMail(Email email) {
		try {
			SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
			// 邮件发送人
			simpleMailMessage.setFrom(USER_NAME);
			// 邮件接收人
			simpleMailMessage.setTo(email.getToUser());
			// 邮件主题
			simpleMailMessage.setSubject(email.getSubject());
			// 邮件内容
			simpleMailMessage.setText(email.getContent());
			mailSender.send(simpleMailMessage);
		} catch (Exception e) {
			logger.error("邮件发送失败", e.getMessage());
		}
	}

	/**
	 * 发送一个HTML格式的邮件
	 *
	 * @param mailBean
	 */
	@Async
	@Override
	public void sendHTMLMail(Email email) {
		MimeMessage mimeMailMessage = null;
		try {
			mimeMailMessage = mailSender.createMimeMessage();
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
			mimeMessageHelper.setFrom(USER_NAME);
			mimeMessageHelper.setTo(email.getToUser());
			mimeMessageHelper.setSubject(email.getSubject());
			StringBuilder sb = new StringBuilder();
			//sb.append("<h1>SpirngBoot测试邮件HTML</h1>").append("\"<p style='color:#F00'>你是真的太棒了！</p>").append("<p style='text-align:right'>右对齐</p>");
			//sb.append("<h1>").append(email.getContent()).append("</h1>");
			sb.append(email.getContent());
			mimeMessageHelper.setText(sb.toString(), true);
			mailSender.send(mimeMailMessage);
			logger.info("邮件发送成功，接收者："+email.getToUser());
		} catch (Exception e) {
			logger.error("邮件发送失败", e);
		}
	}

	/**
	 * 发送带附件格式的邮件
	 *
	 * @param mailBean
	 */
	@Async
	@Override
	public void sendAttachmentMail(Email email) {
		MimeMessage mimeMailMessage = null;
		try {
			mimeMailMessage = mailSender.createMimeMessage();
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
			mimeMessageHelper.setFrom(USER_NAME);
			mimeMessageHelper.setTo(email.getToUser());
			mimeMessageHelper.setSubject(email.getSubject());
			mimeMessageHelper.setText(email.getContent());
			// 文件路径
			FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/img/a6.jpg"));
			mimeMessageHelper.addAttachment("mail.png", file);

			mailSender.send(mimeMailMessage);
		} catch (Exception e) {
			logger.error("邮件发送失败", e.getMessage());
		}
	}

	/**
	 * 发送带静态资源的邮件
	 *
	 * @param mailBean
	 */
	@Async
	@Override
	public void sendInlineMail(Email email) {
		MimeMessage mimeMailMessage = null;
		try {
			mimeMailMessage = mailSender.createMimeMessage();
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
			mimeMessageHelper.setFrom(USER_NAME);
			mimeMessageHelper.setTo(email.getToUser());
			mimeMessageHelper.setSubject(email.getSubject());
			mimeMessageHelper.setText("<html><body>带静态资源的邮件内容，这个一张IDEA配置的照片:<img src='cid:picture' /></body></html>", true);
			// 文件路径
			FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/mail.png"));
			mimeMessageHelper.addInline("picture", file);

			mailSender.send(mimeMailMessage);
		} catch (Exception e) {
			logger.error("邮件发送失败", e.getMessage());
		}
	}

	/**
	 * 发送基于Freemarker模板的邮件
	 *
	 * @param mailBean
	 */
	@Override
	public void sendTemplateMail(Email email) {
		/*MimeMessage mimeMailMessage = null;
		try {
			mimeMailMessage = mailSender.createMimeMessage();
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
			mimeMessageHelper.setFrom(USER_NAME);
			mimeMessageHelper.setTo(email.getToUser());
			mimeMessageHelper.setSubject(email.getSubject());

			Map<String, Object> model = new HashMap<String, Object>();
			model.put("content", email.getContent());
			model.put("title", "ERP找回密码");
			Template template = configuration.getTemplate("mail.ftl");
			String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
			mimeMessageHelper.setText(text, true);

			mailSender.send(mimeMailMessage);
		} catch (Exception e) {
			logger.error("邮件发送失败", e.getMessage());
		}*/

	}
}
